package com.youth.mapper;

import com.youth.entity.po.Order;
import com.youth.entity.vo.OrderVO;
import org.apache.ibatis.annotations.*;
import org.springframework.data.repository.query.Param;

import java.util.List;

@Mapper
public interface IOrderMapper {


    /**
     * 删除 :根据订单id更改订单状态
     *
     * @param orderId int id
     * @return int
     */
    @Update("update tb_order set status=5 where order_id=#{orderId}")
    int deleteOrderById(String orderId);

    /**
     * @Description :新增订单
     * @Param: [record]
     * @Return: int
     * @Author: Administrator
     * @Date: 2019/11/26 15:37
     */
    @Insert("insert into tb_order (order_id,status,create_time,user_id)values (#{orderId},#{status},#{createTime},#{userId})")
    int insert(Order record);

    /**
     * @Description :根据id查找订单
     * @Param: [orderId]
     * @Return: Order
     * @Author: Administrator
     * @Date: 2019/11/26 15:37
     */
    @Select("select t.order_id,t.payment,t.payment_type,t.post_fee,t.status,t.consign_time as isConsign ,t.end_time as isEnd ,t.shipping_name,t.shipping_code,t.user_id," +
            "t.buyer_message,t.buyer_nick,t.buyer_rate " +
            " from tb_order as t  where t.order_id=#{orderId}")
    @Results({@Result(column = "order_id", property = "orderItemVO", one = @One(select = "com.youth.mapper.IOrderItemMapper.getOrderItemById"))})
    OrderVO selectByPrimaryKey(@Param("orderId") String orderId);

    /**
     * 更新订单
     *
     * @param record int id
     * @return int
     */
    @Update("<script>" +
            "update  tb_order " +
            "set update_time=#{updateTime}" +
            "<if test='payment != null'> " + " ,payment =#{payment}" +
            "</if>" +
            "<if test='paymentType != null'> " + ", payment_type =#{paymentType}" +
            "</if>" +
            "<if test='postFee != null'> " + ", post_fee =#{postFee}" +
            "</if>" +
            "<if test='status != null'> " + ", status =#{status}" +
            "</if>" +
            "<if test='paymentTime != null'> " + ", payment_time =#{paymentTime}" +
            "</if>" +
            "<if test='consignTime != null'> " + ", consign_time =#{consignTime}" +
            "</if>" +
            "<if test='endTime != null'> " + ", end_time =#{endTime}" +
            "</if>" +
            "<if test='closeTime != null'> " + ", close_time =#{closeTime}" +
            "</if>" +
            "<if test='shippingName != null'> " + ", shipping_name =#{shippingName}" +
            "</if>" +
            "<if test='shippingCode != null'> " + ", shipping_code =#{shippingCode}" +
            "</if>" +
            "<if test='userId != null'> " + ", user_id =#{userId}" +
            "</if>" +
            "<if test='buyerMessage != null'> " + ", buyer_message =#{buyerMessage}" +
            "</if>" +
            "<if test='buyerNick != null'> " + ", buyer_nick =#{buyerNick}" +
            "</if>" +
            "<if test='buyerRate != null'> " + ", buyer_rate =#{buyerRate}" +
            "</if>" +
            "  where order_id=#{orderId}" +
            "</script>")
    int updateByOrderId(Order record);


    /**
     * @Description :用户订单查询
     * @Param: [userId]
     * @Return: List<Order>
     * @Author: Administrator
     * @Date: 2019/11/26 15:36
     */
    @Select("select * from tb_order where user_id=#{userId} and status!=5")
    List<Order> getOrdersByUserId(String userId);


}